public class MyLinkedList implements ILinkedList{
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int date) {
     ListNode node=new ListNode(date);
        if(head==null){
            head=node;
            last=node;
        }
        else {
            node.next=head;
            head.prev=node;
            head=node;
        }
    }

    @Override
    public void addLast(int date) {
        ListNode node=new ListNode(date);
        if(head==null){
            head=node;
            last=node;
        }
        else {
           last.next=node;
           node.prev=last;
           last=node;
        }
    }

    @Override
    public void addIndex(int index, int date) {
          //1.判断Index的合法性
        if(index<0||index>size()){
            throw new CheckPosException("Index插入位置不合法"+index);
        }
        //2.处理特殊情况
        if (index==0){
            addFirst(date);
            return;
        }
        if (index==size()){
            addLast(date);
            return;
        }
        //普通位置插入
        ListNode node =new ListNode(date);
        ListNode cur=searchIndex(index);
        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;

    }
     private ListNode searchIndex(int index){
        ListNode cur=head;
        while (index!=0){
            cur=cur.next;
            index--;
        }
        return cur;
     }
    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
               return  true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
         ListNode cur=head;
         while (cur!=null){
             if (cur.val==key){
                 if (cur==head){
                     head=head.next;
                     if(head!=null){
                         head.prev=null;
                     }
                 }
                 else {
                     cur.prev.next=cur.next;
                     if(cur.next==null){
                         last=last.prev;
                     }else {
                         cur.next.prev=cur.prev;
                     }

                 }
                 return;
             }
           cur=cur.next;
         }

    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while (cur!=null){
            if (cur.val==key){
                if (cur==head){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }
                }
                else {
                    cur.prev.next=cur.next;
                    if(cur.next==null){
                        last=last.prev;
                    }else {
                        cur.next.prev=cur.prev;
                    }

                }
            }
            cur=cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
       ListNode cur=head;
       while (cur!=null){
           ListNode curN=cur.next;
           cur.prev=null;
           cur.next=null;
           cur=curN;
       }
       head=null;
       last=null;
    }

    @Override
    public void display() {
          ListNode cur=head;
          while (cur!=null){
              System.out.println(cur.val+" ");
              cur=cur.next;
          }
        System.out.println();
    }
}
